package cn.kgc.mes.storage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.kgc.mes.storage.domain.StorageArea;
import cn.kgc.mes.storage.domain.StorageLocation;
import cn.kgc.mes.storage.domain.Warehouse;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.storage.domain.RtIssue;
import cn.kgc.mes.storage.service.IRtIssueService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 生产退料Controller
 * 
 * @author ydw
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/issue/issue")
public class RtIssueController extends BaseController
{
    @Autowired
    private IRtIssueService rtIssueService;

    /**
     * 查询生产退料列表
     */
//    @PreAuthorize("@ss.hasPermi('issue:issue:list')")
    @GetMapping("/list")
    public TableDataInfo list(RtIssue rtIssue)
    {
        startPage();
        List<RtIssue> list = rtIssueService.selectRtIssueList(rtIssue);
        return getDataTable(list);
    }

    /**
     * 导出生产退料列表
     */
//    @PreAuthorize("@ss.hasPermi('issue:issue:export')")
    @Log(title = "生产退料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RtIssue rtIssue)
    {
        List<RtIssue> list = rtIssueService.selectRtIssueList(rtIssue);
        ExcelUtil<RtIssue> util = new ExcelUtil<RtIssue>(RtIssue.class);
        util.exportExcel(response, list, "生产退料数据");
    }

    /**
     * 获取生产退料详细信息
     */
//    @PreAuthorize("@ss.hasPermi('issue:issue:query')")
    @GetMapping(value = "/{rtId}")
    public AjaxResult getInfo(@PathVariable("rtId") Long rtId)
    {
        return success(rtIssueService.selectRtIssueByRtId(rtId));
    }

    /**
     * 新增生产退料
     */
//    @PreAuthorize("@ss.hasPermi('issue:issue:add')")
    @Log(title = "生产退料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RtIssue rtIssue)
    {
        System.out.println("退料");
        System.out.println(rtIssue);
        return toAjax(rtIssueService.insertRtIssue(rtIssue));
    }

    /**
     * 修改生产退料
     */
//    @PreAuthorize("@ss.hasPermi('issue:issue:edit')")
    @Log(title = "生产退料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RtIssue rtIssue)
    {
        return toAjax(rtIssueService.updateRtIssue(rtIssue));
    }

    /**
     * 删除生产退料
     */
//    @PreAuthorize("@ss.hasPermi('issue:issue:remove')")
    @Log(title = "生产退料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rtIds}")
    public AjaxResult remove(@PathVariable Long[] rtIds)
    {
        return toAjax(rtIssueService.deleteRtIssueByRtIds(rtIds));
    }
    //    仓库联查
    @GetMapping("cangku")
    public Object selectRtIssueList(){
        List<RtIssue> list = rtIssueService.selectRtIssueList1();
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("data",list);
        }else{
            map.put("code",0);
            map.put("msg","查询失败");
        }
        return map;
    }
    //根据id查询仓库数据
    @GetMapping("cangku1")
    public Object selectRtIssueList1(@RequestParam("id") Long id){
        Warehouse list = rtIssueService.WarehouseById(id);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("data",list);
        }else{
            map.put("code",0);
            map.put("msg","查询失败");
        }
        return map;
    }
    //根据id查询库区数据
    @GetMapping("qu")
    public Object selectLocation(@RequestParam("id") Long id){
        StorageLocation list = rtIssueService.StorageLocationById(id);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("data",list);
        }else{
            map.put("code",0);
            map.put("msg","查询失败");
        }
        return map;
    }
    //根据id查询库位数据
    @GetMapping("wei")
    public Object selectArea(@RequestParam("id") Long id){
        StorageArea list = rtIssueService.StorageAreaById(id);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("data",list);
        }else{
            map.put("code",0);
            map.put("msg","查询失败");
        }
        return map;
    }
}
